package hu.genius.view.adapters;

import hu.genius.R;
import hu.genius.model.entity.Order;
import hu.genius.view.utils.ViewUtils;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BasketItemAdapter extends ArrayAdapter<Order> implements OnClickListener {
	private List<Order> orders;

	public BasketItemAdapter(Context context, int resource,
			int textViewResourceId, List<Order> objects) {
		super(context, resource, textViewResourceId, objects);
		this.orders = objects;
	}

	public BasketItemAdapter(Context context, int textViewResourceId,
			List<Order> objects) {
		super(context, textViewResourceId, objects);
		this.orders = objects;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.listview_basket_item_layout, null);
		}

		Order order = orders.get(position);
		if (order != null) {
			TextView orderProductNameTextView = (TextView) v
					.findViewById(R.id.orderProductNameTextView);
			TextView orderProductPrice = (TextView) v
					.findViewById(R.id.orderProductPriceTextView);
			TextView orderQuantity = (TextView) v
					.findViewById(R.id.orderQuantityTextView);

			ImageView orderImageView = (ImageView) v
					.findViewById(R.id.orderImageView);
			
			Bitmap bitmap = null;
			if (order.getProduct().getImage() != null) {
				bitmap = ViewUtils.downloadImage(order.getProduct().getImage());
			} else {
				bitmap = ViewUtils.loadImage(
						this.getContext(), order.getProduct().getId() + "s110c"
								+ order.getProduct().getPrice() + ".00i");
			}
			
			orderImageView.setImageBitmap(bitmap);
			
			orderImageView.setLayoutParams(new LinearLayout.LayoutParams(
					ViewUtils.getDpSize(this.getContext(), 110), ViewUtils
							.getDpSize(this.getContext(), 110)));

			Button plusButton = (Button) v.findViewById(R.id.orderPlusButton);
			plusButton.setOnClickListener(this);
			
			Button minusButton = (Button) v.findViewById(R.id.orderMinusButton);
			minusButton.setOnClickListener(this);
			
			if (orderProductNameTextView != null) {
				orderProductNameTextView.setText(order.getProduct().getName());
			}

			if (orderProductPrice != null) {
				orderProductPrice.setText((order.getProduct().getPrice() * order.getQuantity()) + " Ft");
			}

			if (orderQuantity != null) {
				orderQuantity.setText(order.getQuantity() + " db");
			}
			
		}
		return v;
	}

	public void onClick(View v) {
		if (v != null) {
			switch (v.getId()) {
			case R.id.orderPlusButton: {
				break;
			}
			case R.id.orderMinusButton: {
				break;
			}
			}
		}
	}

}
